from web import template

template.Template.globals['completed_bytes'] = 0
template.Template.globals['size'] = 0
template.Template.globals['down_rate'] = 0
template.Template.globals['up_rate'] = 0

def global_stat(completed_bytes = 0, bytes = 0, down_rate = 0, up_rate = 0,
                    return_completed_bytes = 0, return_bytes = 0,
                    return_down_rate = 0, return_up_rate = 0):
    template.Template.globals['completed_bytes'] += abs(completed_bytes)
    template.Template.globals['size'] += abs(bytes)
    template.Template.globals['down_rate'] += abs(down_rate)
    template.Template.globals['up_rate'] += abs(up_rate)
    if return_completed_bytes != 0:
        return template.Template.globals['completed_bytes']
    if return_bytes != 0:
        return template.Template.globals['size']
    if return_down_rate != 0:
        return template.Template.globals['down_rate']
    if return_up_rate != 0:
        return template.Template.globals['up_rate']
template.Template.globals['global_stat'] = global_stat

def clear_stat():
    template.Template.globals['completed_bytes'] = 0
    template.Template.globals['size'] = 0
    template.Template.globals['down_rate'] = 0
    template.Template.globals['up_rate'] = 0
template.Template.globals['clear_stat'] = clear_stat
